package cn.ito.zjgsu.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ito.zjgsu.domain.Authority;
import cn.ito.zjgsu.domain.AuthorityExample;
import cn.ito.zjgsu.domain.Role;
import cn.ito.zjgsu.domain.RoleAuthority;
import cn.ito.zjgsu.domain.RoleAuthorityExample;
import cn.ito.zjgsu.domain.RoleExample;
import cn.ito.zjgsu.domain.UserLogin;
import cn.ito.zjgsu.domain.UserLoginExample;
import cn.ito.zjgsu.domain.wrapper.AuthorityWrapper;
import cn.ito.zjgsu.domain.wrapper.RoleWrapper;
import cn.ito.zjgsu.mapping.AuthorityMapper;
import cn.ito.zjgsu.mapping.RoleAuthorityMapper;
import cn.ito.zjgsu.mapping.RoleMapper;
import cn.ito.zjgsu.service.RolePermissionService;
import cn.ito.zjgsu.utils.util.CollectionUtil;

@Service
@Transactional
public class RolePermissionServiceImpl implements RolePermissionService{
	
	@Resource
	private AuthorityMapper authorityMapper;
	
	@Resource
	private RoleMapper roleMapper;
	
	@Resource
	private RoleAuthorityMapper roleAuthorityMapper;

	
	
	
	
	@Override
	public List<Authority> getPermissions(int rid) {
		List<Integer> pids = getPids(rid);
		if(CollectionUtil.isSafeEmpty(pids)){
			return null;
		}else{
			return getPermissionsByPids(pids);
		}	
	}

	

	@Override
	public Role getRole(int rid) {
		RoleExample roleExample = new RoleExample();
		roleExample.or().andRoleIdEqualTo(rid).andRoleDeleteTagsEqualTo(Role.ROLE_EXISTS_FLAG);
		
		return roleMapper.selectByExample(roleExample).isEmpty()? null : roleMapper.selectByExample(roleExample).get(0);
	}

	
	@Override
	public RoleWrapper getRoleWrapper(UserLogin user) {
		RoleWrapper roleWrapper = new RoleWrapper();
		roleWrapper.setRole(getRole(user.getRoleId()));
		roleWrapper.setPermissions(AuthorityWrapper.WrapAuthority(getPermissions(user.getRoleId())));
		return roleWrapper;
	}
	
	@Override
	public RoleWrapper getRoleWrapper(Integer rid) {
		if(null != rid){
			RoleWrapper roleWrapper = new RoleWrapper();
			roleWrapper.setRole(getRole(rid));
			roleWrapper.setPermissions(AuthorityWrapper.WrapAuthority(getPermissions(rid)));
			return roleWrapper;
		}else{
			return new RoleWrapper();
		}
		
	}
	
	
	
	
	
	
	//内部方法
	
	private List<Authority> getPermissionsByPids(List<Integer> pids) {
		AuthorityExample authorityExample = new AuthorityExample();
		authorityExample.or().andAuthorityIdIn(pids);
		return authorityMapper.selectByExample(authorityExample);
	}

	private List<Integer> getPids(int rid) {
		RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
		roleAuthorityExample.or().andRoleIdEqualTo(rid).andRaDeleteTagsEqualTo(RoleAuthority.RA_EXISTS_FLAG);
		roleAuthorityExample.setDistinct(true);
		List<RoleAuthority> roleAuthorities = roleAuthorityMapper.selectByExample(roleAuthorityExample);
		List<Integer> pids = new ArrayList<Integer>();
		for(RoleAuthority ra : roleAuthorities){
			pids.add(ra.getAuthorityId());
		}
		return pids;
	}



	


	public int getRaidByRid(int rid){
		RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
		roleAuthorityExample.or().andRaDeleteTagsEqualTo(RoleAuthority.RA_EXISTS_FLAG)
		.andRoleIdEqualTo(rid);	
		RoleAuthority roleAuthority = roleAuthorityMapper.selectByExample(roleAuthorityExample).get(0);	
		return roleAuthority.getRaId();	
	}



	@Override
	public List<AuthorityWrapper> getPermissionsWrapper(int rid) {
		return AuthorityWrapper.WrapAuthority(getPermissions(rid));
	}



	

	



	

}
